home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 April / macformat-023.iso / Shareware City / Developers / cookie folder / cookie.c next >
Encoding:
Text File  |  1994-10-28  |  2.8 KB  |  142 lines  |  [TEXT/KAHL]

  1.  
  2. // ================================================================================
  3. //
  4. //    cookie.c - a fortune cookie command for the Mac
  5. //    
  6. //    Copyright (c) 1994 James  E. O'Hearn
  7. //    
  8. //    You may distribute unmodified copies of this file for
  9. //    noncommercial purposes.  All other rights are reserved.
  10. //
  11. // ================================================================================
  12.  
  13. // Constants
  14.  
  15. #define BASE_STRING 16000
  16.  
  17. // CodeWarrior requires an A4 setup
  18.  
  19. #ifdef __MWERKS__
  20. #include <A4Stuff.h>
  21. #endif
  22.  
  23. // Read standard nShell includes
  24.  
  25. #include "nshc.h"
  26. #include "nshc_utl.proto.h"
  27. #include "str_utl.proto.h"
  28.  
  29. // Prototypes
  30.  
  31. short OpenMyResources (t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls);
  32. OSErr DoCookie (t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls);
  33.  
  34. // ================================================================================
  35.  
  36. void main (t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls)
  37. {
  38.  
  39.     OSErr    error;
  40.  
  41.  
  42. #ifdef __MWERKS__
  43.     long oldA4  = SetCurrentA4();
  44. #endif
  45.     
  46.     if (!nshc_bad_version (nshc_parms, nshc_calls, NSHC_VERSION))
  47.     {
  48.     
  49.         // otherwise, handle requests from the application
  50.     
  51.           switch (nshc_parms->action)
  52.           {
  53.             case nsh_start:
  54.                 error = DoCookie (nshc_parms, nshc_calls);
  55.                 break;
  56.             case nsh_continue:
  57.             case nsh_idle:
  58.             case nsh_stop:
  59.                 error = NSHC_NO_ERR;
  60.                 break;
  61.         }
  62.         
  63.         // tell the application we are done
  64.  
  65.          nshc_parms->action = nsh_idle;
  66.         nshc_parms->result = error;
  67.  
  68.     }
  69.         
  70. #ifdef __MWERKS__
  71.     SetA4(oldA4);        // CodeWarrior needs to restore A4
  72. #endif
  73. }
  74.  
  75. // ================================================================================
  76.  
  77. OSErr DoCookie (t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls)
  78. {
  79.  
  80.     short             numStrings,theString,fileRef;
  81.     StringHandle    string;
  82.     
  83.     
  84.     fileRef = OpenMyResources (nshc_parms, nshc_calls);
  85.     
  86.     if (fileRef < 0)
  87.     {
  88.         nshc_calls->NSH_putStr_err ("\pcookie: Could not open resource file.\r");
  89.         return (NSHC_ERR_GENERAL);
  90.     }
  91.     
  92.     numStrings = Count1Resources ('STR ');
  93.     
  94.     if (!numStrings)
  95.     {
  96.         nshc_calls->NSH_putStr_err ("\pcookie: no string resources found.\r");
  97.         return (NSHC_ERR_GENERAL);
  98.     }
  99.         
  100.     theString = BASE_STRING + (TickCount() % numStrings);
  101.     
  102.     string = GetString (theString);
  103.     
  104.     if (!string)
  105.     {
  106.         nshc_calls->NSH_putStr_err ("\pcookie: string could not be loaded.\r");
  107.         return (NSHC_ERR_GENERAL);
  108.     }
  109.     
  110.     HLock (string);
  111.     nshc_calls->NSH_putStr (*string);
  112.     HUnlock (string);
  113.     
  114.     ReleaseResource (string);
  115.     CloseResFile (fileRef);
  116.  
  117.     return (NSHC_NO_ERR);
  118.     
  119. }
  120.  
  121. // ================================================================================
  122.  
  123. short OpenMyResources (t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls)
  124. {
  125.  
  126.     Str255    fileName;
  127.     short    fileRef;
  128.  
  129.  
  130.     pStrFromC (fileName, &nshc_parms->arg_buf[nshc_parms->argv[0]]);
  131.     
  132.     fileRef = -1;
  133.     
  134.     if ( !nshc_calls->NSH_path_which (fileName))
  135.     {
  136.         fileRef = OpenResFile (fileName);
  137.     }
  138.         
  139.     return (fileRef);
  140. }
  141.  
  142.